package com.bum.api.core.util.crypt;

import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;

public class RSIUtil {
	private static Random	r	= new Random(System.currentTimeMillis());

	public static byte[] generateRSI() {
		byte offset = (byte) r.nextInt(15);
		byte rsi[] = new byte[20 + offset];
		r.nextBytes(rsi);
		rsi[0] = offset;
		return rsi;
	}

	private static String md5(byte[] pass) {
		return DigestUtils.md5Hex(pass);
	}

	public static byte[] encrypt(byte key[], String password) {
		if (key == null || key.length == 0 || password == null || password.length() == 0) {
			return null;
		}

		byte[] pass;
		try {
			pass = password.getBytes("UTF-8");
		} catch (Exception e) {
			return null;
		}

		byte[] pass1 = crypt(key, pass);
//		System.out.println("pub + pass: " + Arrays.toString(pass1));

		byte[] md5 = md5(pass).getBytes();
		byte[] pass2 = crypt(md5, pass1);

		return pass2;
	}

	private static byte[] crypt(byte key[], byte pass[]) {
		if (key == null || key.length == 0 || pass == null || pass.length == 0) {
			return null;
		}

		byte[] result = (byte[]) null;
		int offset = key[0];

		result = new byte[Math.max(key.length - 1, pass.length + offset) + 1];
		result[0] = (byte) pass.length;
		for (int i = 0; i < result.length - 1; i++) {
			byte b1 = 17;
			byte b2 = 0;
			if (i + 1 < key.length) {
				b1 = key[i + 1];
			}
			if (i >= offset && i - offset < pass.length) {
				b2 = pass[i - offset];
			}
			result[i + 1] = (byte) (b1 ^ b2);
		}

		return result;
	}

	public static byte[] decrypt(byte[] key, byte[] encryptedPassword) {
		if (encryptedPassword == null || encryptedPassword.length == 0 || key == null || key.length == 0 || encryptedPassword[0] <= 0) {
			return null;
		}

		int passLength = encryptedPassword[0];
		int offset = key[0];
		byte[] pass = new byte[passLength];

		for (int i = offset; i < pass.length + offset; i++) {
			byte b1 = 17;
			byte b2 = 0;
			if (i + 1 < key.length) {
				b1 = key[i + 1];
			}
			b2 = encryptedPassword[i + 1];
			pass[i - offset] = (byte) (b1 ^ b2);
		}
		return pass;
	}

//	public static void main(String[] args) {
//		byte[] rs = new byte[] {
//				30, 55, 51, 55, 55, 51, 51, 54, 55, 100, 50, 50, 50, 97, 102, 52, 50, 57, 97, 55, 101, 53, 97, 98, 53, 55, 51, 97, 52, 50, 100, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
//				17, 17, 17, 17, 17, 17, 17, 21, 7, -23, -108, 77, 31, 101, 44, 107, -112, -116, 82, -29, 93, 127, 31, -31, -94, -58, -24, 127, 12, 14, -17, 36, 19, -115, 111, -82, 4
//		};
//		byte[] pub = new byte[] {
//				10, 22, -8, -123, 92, 14, 116, 61, 122, -127, -99, 122, -57, 125, 93, 14, -16, -77, -41, -7, 110, 29, 31, -2, 53, 2, -100, 126, -65, 21
//		};
//
//		byte[] midPass = new byte[] {
//				0, 0, 0, 4, 22, -8, -123, 92, 14, 116, 61, 122, -127, -99, 67, -14, 76, 110, 14, -16, -77, -41, -7, 110, 29, 31, -2, 53, 2, -100
//		};
//
//		byte[] enc = encrypt(pub, "9513");
//		System.out.println("Enc rs:" + Arrays.toString(enc));
//
//		byte[] passwordEncrypted = enc;
//		passwordEncrypted = rs;
//		
//		String passMd5 = md5("9513".getBytes());
//		passMd5 = passMd5.replaceAll("^[0]+", "");
//		System.out.println(passMd5);
//		byte[] md5Bytes = passMd5.getBytes();
//		System.out.println("md5Bytes: " + Arrays.toString(md5Bytes));
//		
//		byte[] pass1 = RSIUtil.decrypt(md5Bytes, passwordEncrypted);
//		System.out.println("decr pass1: " + Arrays.toString(pass1));
//
//		byte[] pubKey = pub;
//
//		byte[] pass0 = RSIUtil.decrypt(pubKey, pass1);
//		System.out.println(new String(pass0));
//
////		System.out.println(md5("7199".getBytes()));
//	}
}
